<template>
  <div style="padding:50px 0;width:45%">
    <el-form ref="form" :model="form" :rules="rule" label-width="260px">
      <el-form-item label="姓 名：" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密 码：" prop="password">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onUpdate">更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById, updateDetailById } from '@/api/user'
export default {
  name: '',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rule: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      }
    }
  },
  computed: {
  },
  created() {
    this.initUserInfo()
  },
  methods: {
    async initUserInfo() {
      const { data: { username, password }} = await getUserDetailById(this.$route.query.id)
      //   console.log(res)
      this.form = { username, password }
    },
    onUpdate() {
      // 兜底校验
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const id = {
          id: this.$route.query.id
        }
        try {
          console.log(this.form)
          await updateDetailById({ ...this.form, ...id })
          this.$message.success('修改成功')
        } catch (err) {
          console.log(err)
        }
      })
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
